package StudyCode.summary.myfile;

import java.io.IOException;
import java.nio.file.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class DisplayDiskSpaceInformation {
	private static final long K = 1024;

	public static void main(String[] args) throws IOException {
		System.out.format("%-20s %12s %12s %12s\n", "分区", "kBytes", "已用空间", "可用空间");
		if (args.length == 0) {
			FileSystem fs = FileSystems.getDefault();
			for (FileStore store : fs.getFileStores()) {
				printFileStore(store);
			}
		} else {
			for (String file : args) {
				FileStore store = Files.getFileStore(Paths.get(file));
				printFileStore(store);
			}
		}
	}

	private static void printFileStore(FileStore store) throws IOException {
		long total = store.getTotalSpace() / K;
		long used = (store.getTotalSpace() - store.getUnallocatedSpace())
				/ K;
		long avail = store.getUsableSpace() / K;

		String s = store.toString();
		if (s.length() > 20) {
			System.out.println(s);
			s = "";
		}
		System.out.format("%-20s %12d %12d %12d\n", s, total, used, avail);
	}
}
